#include "filterCounter.h"
#include "sc95f_iap.h"
#include "SMProcess.h"
#include "stdio.h"

extern uint8_t BTMCount_1S;
extern xdata uint32_t BTMCount_1min;

extern uint32_t xdata eventValue;
/*********************适配器接口***********************************/
//bool xdata TimeWriteFlag = FALSE;
bool filterSwitch = FALSE;
volatile FILTER_INPUT filter_i_evt= NONE;
volatile FILTER_OUTPUT filter_o_evt;
volatile bool filter_write_flag = FALSE;


void filter_input_adapter(uint8_t *m, FILTER_INPUT evt)
{
	(*m) = evt;
}

void filter_state_set(FILTER_INPUT value)
{
	filter_i_evt = value;
}
/* simple */
//filter_input_adapter(&filter_i_evt,Boot);
FILTER_OUTPUT filter_output_adapter()
{
	return filter_o_evt;
}

bool fileter_ONOFF()
{
	return filterSwitch;
}
/***********************end***********************************/

typedef struct _filter_t
{
	uint8_t Sign;
	uint32_t remainFilterLife;
} filter_t;

static filter_t filter =
{
	0xA5,
	filterLifeMax
};

typedef enum
{
	eFilterNone = 0,
	eFilterInit,
	eFilterWrite,
	eFilterMAX
} eFILTER_STATUS;

static uint32_t decrement_life_time(uint32_t time, uint32_t remaintime)
{
	if (remaintime > time)
	{
		remaintime -= time;
	}
	else
	{
		remaintime = 0;
	}
	return remaintime;
}

static void filterCountClear(void)
{
	BTMCount_1S = 0;
	BTMCount_1min = 0;
}

/*flash 读写清除驱动*/
static void filter_time_read(uint32_t add)
{
	IAP_ReadByteArray(add, (uint8_t *)&filter, sizeof(filter), IAP_MEMTYPE_EEPROM);
}

static void filter_time_clear(uint32_t add)
{
	IAP_SectorErase(IAP_MEMTYPE_EEPROM, add, 0xF0); // Erase
}

static uint8_t filter_time_write(uint32_t add)
{
	return IAP_ProgramByteArray(add, (uint8_t *)&filter, sizeof(filter), IAP_MEMTYPE_EEPROM, 0xF0);
}
/*end*/

void filter_speed_up_init(void)
{
	xdata uint16_t Length_t = 0;
	filter.Sign = 0xA5;
	filter.remainFilterLife = 60;
	filter_time_clear(WriteSector);
	Length_t = filter_time_write(WriteAddress);
	if (Length_t == sizeof(filter))
	{
		//printf("remainFilterLife:%lu min\r\n", filter.remainFilterLife);
	}
}

static eFILTER_STATUS filter_poweron_init(filter_t *me)
{
	eFILTER_STATUS evt = eFilterNone;
	if (me == NULL)
	{
		return eFilterNone;
	}
	if (me->Sign != 0xA5) // 未初始化
	{
		evt = eFilterInit;
	}
	else if (me->remainFilterLife == 0x00) // 滤网到期
	{
		filterSwitch = FALSE; // 关闭计数
		filter_o_evt = Alarm;
		eventValue = SYS_FILTER_TIMEOUT_EVENT;
		//printf("->Filter Tiem Out<-\r\n");
	}
	return evt;
}

static void filter_count_cmd(eFILTER_STATUS Status)
{
	// if(Status > eFilterMAX)
	// {
	// 	return;
	// }
	xdata uint16_t Length_t = 0;
	switch (Status)
	{
	case eFilterWrite:
		//printf("->Filter Write status<-\r\n");
		filter_time_read(WriteAddress);
		filter.remainFilterLife = decrement_life_time(BTMCount_1min, filter.remainFilterLife);
		filter_time_clear(WriteSector);
		Length_t = filter_time_write(WriteAddress);
		if (Length_t == sizeof(filter))
		{
			printf("remainFilterLife:%lu min\r\n", filter.remainFilterLife);
		}
		if (filter.remainFilterLife == 0x00)
		{
			printf("->Filter Tiem Out<-\r\n");
			filterSwitch = FALSE; // 关闭计数
			filter_o_evt = Alarm;
			eventValue = SYS_FILTER_TIMEOUT_EVENT;
		}
		filterCountClear(); // clear
		break;

	case eFilterInit:
		//printf("->Filter reset status<-\r\n");
		filterCountClear(); // clear
		filter.Sign = 0xA5;
		filter.remainFilterLife = filterLifeMax;
		filter_time_clear(WriteSector);
		Length_t = filter_time_write(WriteAddress);
		printf("Device filter initialization,Set time :%lu \r\n", filter.remainFilterLife);
		filter_o_evt = Recover;
		filterSwitch = TRUE;
		break;
	default:
		break;
	}
}

void filter_count_handle(void)
{
	static uint8_t f_step = 0;
	eFILTER_STATUS eFilterStatus = eFilterNone;
	if (f_step == 0)
	{
		if (filter_i_evt == Boot) // 开机
		{
			//printf("filter boot\r\n");
			filter_time_read(WriteAddress);
			eFilterStatus = filter_poweron_init(&filter);
			if (eFilterStatus == eFilterNone)
			{
				f_step = 1;
				filterSwitch = TRUE; // 开始计数
			}
		}
	}
	if (f_step == 1)
	{
		if (filter_write_flag == TRUE)
		{
			eFilterStatus = eFilterWrite;
			filter_write_flag = FALSE;	
			//printf("filter write\r\n");
		}
		if (filter_i_evt == Reset)
		{
			eFilterStatus = eFilterInit;
			//printf("filter reset\r\n");
		}
		if (filter_i_evt == Shutdown)
		{
			filterSwitch = FALSE; // 关闭计数,保留未达到计数时间的值
			f_step = 0;
			//printf("filter shutdown\r\n");
		}
	}
	filter_i_evt = NONE; // 清除事件
	/*  */
	filter_count_cmd(eFilterStatus);

}